class Solution {
public:
    vector<int> ma[100005];
    int num = 0;
    int countGoodNodes(vector<vector<int>>& edges) {
        for (auto& x : edges)
        {
            ma[x[1]].push_back(x[0]);
            ma[x[0]].push_back(x[1]);
        }
        dfs(0, -1);
        return num;
    }
    int dfs(int pos, int pre)
    {
        int ans = 1;
        int p = -1;
        int flag = 1;
        for (auto& x : ma[pos])
        {
            if (x == pre) continue;
            int temp = dfs(x, pos);
            if (p == -1) p = temp;
            else
            {
                if (p != temp) flag = 0;
            }
            ans += temp;
        }
        num += flag;
        return ans;
    }
};